package lumis.service.wsrp.consumer.doui;

import java.util.Set;

import lumis.doui.processaction.ProcessActionHandler;
import lumis.doui.source.Source;
import lumis.doui.source.field.ISourceField;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.manager.ManagerFactory;
import lumis.service.wsrp.consumer.producermanager.ClonedPortlet;
import lumis.service.wsrp.consumer.producermanager.ProducerEntityManagerFactory;
import lumis.util.XmlUtil;

import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;

/**
 * {@link ProcessActionHandler} that stores remote portlet handle in the caller
 * service interface instance custom property (
 * {@link RemotePortletConfigurationInterfaceConstants#REMOTE_PORTLET_HANDLE_CUSTOM_PROPERTY_ID}
 * ).
 * 
 * @version $Revision: 12864 $ $Date: 2011-04-29 16:18:20 -0300 (Fri, 29 Apr 2011) $
 * @since 5.5.0
 */
public class RemotePortletConfigurationInterfaceProcessActionHandler extends ProcessActionHandler<Source<ISourceField>>
{
	private static final String ACTION_SELECT_REMOTE_PORTLET = "selectRemotePortlet";
	private static final String ACTION_REFRESH_SERVICE_DESCRIPTION = "refreshServiceDescription";
	
	public void processAction() throws PortalException
	{
		String actionType = XmlUtil.readAttributeString("actionType", processActionNode);
		if (ACTION_SELECT_REMOTE_PORTLET.equals(actionType))
			processSelectRemotePortlet();
		else if (ACTION_REFRESH_SERVICE_DESCRIPTION.equals(actionType))
			processRefreshServiceDescription();
	}

	private void processRefreshServiceDescription() throws PortalException
	{
		ProducerEntityManager producerEntityManager = ProducerEntityManagerFactory.getProducerEntityManager(douiContext.getRequest()
				.getServiceInstanceConfig().getId());
		try
		{
			Set<String> producerEntityIds = producerEntityManager.getProducerEntityIds();
			if (producerEntityIds.isEmpty())
				throw new PortalException("STR_CONSUMER_NOT_CONFIGURED", getResource());

			for (String producerEntityId : producerEntityIds)
				producerEntityManager.updateServiceDescription(producerEntityId);
		}
		catch (WSRPConsumerException e)
		{
			throw new UnexpectedException(e);
		}
	}

	private void processSelectRemotePortlet() throws PortalException
	{
		Object objPortletHandle = getParameter(RemotePortletConfigurationInterfaceConstants.REMOTE_PORTLET_HANDLE_PARAMETER_NAME);
		if (objPortletHandle == null)
			throw new PortalException(localize("STR_PORTLET_HANDLE_REQUIRED"));

		String portletHandle;
		if (objPortletHandle instanceof String[])
			portletHandle = (String) ((String[]) objPortletHandle)[0];
		else if (objPortletHandle instanceof String)
			portletHandle = (String) objPortletHandle;
		else
			portletHandle = null;

		if (portletHandle == null || portletHandle.length() == 0)
			throw new PortalException(localize("STR_PORTLET_HANDLE_REQUIRED"));


		String interfaceInstanceId = douiContext.getRequest().getCallerServiceInterfaceInstanceId();
		
		ManagerFactory.getServiceInterfaceInstanceManager().setCustomProperty(sessionConfig,
				interfaceInstanceId,
				RemotePortletConfigurationInterfaceConstants.REMOTE_PORTLET_HANDLE_CUSTOM_PROPERTY_ID, portletHandle, transaction);

		addInterfaceToPortlet(portletHandle, interfaceInstanceId);
		
		addDefaultResponse();
	}
	
	/**
	 * Add the interface instance id to the portlet if it is a cloned portlet.
	 * @param portletHandle			the portlet handle.
	 * @param interfaceInstanceId	the interface instance id.
	 */
	private void addInterfaceToPortlet(String portletHandle, String interfaceInstanceId) throws PortalException
	{
		// removes the old cloned portlet if there is one
		ManagerFactory.getEntityManager()
			.createNativeQuery("delete from lum_WsrpCClonedTgtInst where interfaceInstanceId = '" + interfaceInstanceId + "'")
			.executeUpdate();

		// adds the interface instance to the selected portlet
		ClonedPortlet clonedPortlet = ManagerFactory.getEntityManager().find(ClonedPortlet.class, portletHandle);
		if (clonedPortlet != null)
		{
			clonedPortlet.addInterfaceInstanceId(interfaceInstanceId);
		}
	}
}